package org.pewpew.client;


import java.util.ArrayList;
import java.util.List;

import org.pewpew.model.WorldState;

//TODO should not be static class
public class GameModel {
	private WorldState currentWorldState = new WorldState ();
	private String playerName = null; 
	
	private List<WorldObserver> listeners = new ArrayList<WorldObserver> ();
	
	public GameModel(String playerName) {
		this.playerName = playerName;
	}

	public void addListener (WorldObserver listener) {
		listeners.add(listener);
	}

	public String getPlayerName () {
		return playerName;
	}
	
	public void updateWorldState(String jsonString) {
		WorldState newWorldState = WorldState.fromJSON(jsonString);
		for (WorldObserver observer : listeners) {
			observer.update(this, newWorldState);
		}
		currentWorldState = newWorldState;
	}

	//TODO synchronize
	public WorldState getWorldState() {
		return currentWorldState;
	}

}
